<?php
/**
 * Created by PhpStorm.
 * User: ly
 * Date: 2017\12\25 0025
 * Time: 10:26
 */

use yii\helpers\Html;
use yii\helpers\Url;
use yii\widgets\ActiveForm;
use yii\bootstrap\Alert;

//$this->registerCssFile('@web/css/index.css', ['depends' => ['backend\assets\AppAsset'], 'position' => $this::POS_HEAD]);
$this->registerCssFile('@web/css/mask.css', ['depends' => ['backend\assets\AppAsset'], 'position' => $this::POS_HEAD]);
$this->registerCssFile('@web/css/AssetPackageDetails.css', ['depends' => ['backend\assets\AppAsset'], 'position' => $this::POS_HEAD]);
$this->registerCssFile('@web/css/notEdit.css', ['depends' => ['backend\assets\AppAsset'], 'position' => $this::POS_HEAD]);
$this->registerCssFile('@web/css/paging.css', ['depends' => ['backend\assets\AppAsset'], 'position' => $this::POS_HEAD]);

if(!isset($no_ajax)) $no_ajax=false;
if( Yii::$app->getSession()->hasFlash('success') ) {
    echo Alert::widget([
        'options' => [
            'class' => 'alert-success', //这里是提示框的class
        ],
        'body' => Yii::$app->getSession()->getFlash('success'), //消息体
    ]);
}
if( Yii::$app->getSession()->hasFlash('error') ) {
    echo Alert::widget([
        'options' => [
            'class' => 'alert-error',
        ],
        'body' => Yii::$app->getSession()->getFlash('error'),
    ]);
}
if(isset($packageid)) {
    ?>

    <!-- Modal -->
    <div class="modal-dialog" role="document" <?php if($no_ajax)echo 'style="margin: 5px 5px;"';?> >
        <?php $form = ActiveForm::begin([
            'action' => ['package/set-permissions'], //提交地址(*可省略*)
            'method' => 'post',    //提交方法(*可省略默认POST*)
            'id' => 'set-permissions', //设置ID属性
            'options' => [
                'class' => 'myForm form-inline', //设置class属性
            ]
        ]); ?>
        <div class="modal-content">
            <div class="modal-header">
                <?php
                if(!$no_ajax) {
                    ?>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                                aria-hidden="true"><img src="/image/assetPackage/close.png"></span></button>
                    <?php
                }
                    ?>
                <div class="pull-left modal-nav">
                    <img src="/image/assetPackage/white.png" class="pull-left">
                    <span class="pull-left">修改访问权限</span>
                </div>
            </div>
            <div class="modal-body">
                <div class="modal-body-div"  <?php if ($access == 0 or $access==1) echo 'style="height:100px;"';?> >
                    <input type="hidden" name="packageid" value="<?php echo $packageid; ?>"/>
                    <input type="hidden" name="no_ajax" id="no_ajax" value="<?php echo ($no_ajax)?$no_ajax:0; ?>"/>
                    <!-- 访问权限 -->
                    <div class="form-group jurisdiction">
                        <span for="exampleInputEmail2">访问权限</span>
                        <input type="radio" id="open" name="Package[access]"
                               value="0" <?php if ($access == 0) echo 'checked'; ?> />
                        <label for="open" <?php if ($access == 0) echo 'class="radioChecked" '; ?>><span></span>公开</label>
                        <!--<input type="radio" id="private" name="Package[access]"
                               value="1" <?php if ($access == 1) echo 'checked'; ?> />
                        <label for="private" <?php if ($access == 1) echo 'class="radioChecked" '; ?>><span></span>私密</label>-->
                        <input type="radio" id="partialDisclosure" name="Package[access]"
                               value="2" <?php if ($access == 2) echo 'checked'; ?> />
                        <label for="partialDisclosure" <?php if ($access == 2) echo 'class="radioChecked" '; ?>><span></span>部分公开</label>
                    </div>
                    <!-- 公开部门 -->
                    <div class="form-group clearfix PublicSector"  <?php if ($access == 0 or $access==1) echo 'style="visibility: hidden;" ';?> >
                        <label for="" class="pull-left control-label">公开部门</label>
                        <div class="pull-left label-div">
                            <!-- 部门人员 -->
                            <?php if (!empty($departments) && is_array($departments)) { ?>
                                <?php foreach ($departments as $key => $value) { ?>
                                    <input type="checkbox" checked name="Package[department][]" class="sector"
                                           id="project<?php echo $key + 1; ?>" value="<?php echo $key + 1; ?>"/>
                                    <label style="color: rgb(0, 0, 0);"
                                           for="project<?php echo $key + 1; ?>"><?php echo $value['department'] ?></label>
                                    <br/>

                                    <div class="list_project<?php echo $key + 1; ?>  sectorList">
                                        <input type="checkbox" class="whole" name="Package[access_users][]" value="0"/>
                                        <label for="oneWhole2">全选</label>
                                        <br/>
                                        <?php if (!empty($value['users']) && is_array($value['users'])) { ?>
                                            <?php foreach ($value['users'] as $k => $v) { ?>
                                                <label for="<?php echo $v['id']; ?>">
                                                    <input type="checkbox" class="access_users"
                                                           name="Package[access_users][]"
                                                        <?php if ($access_users_ids!="" && is_array($access_users_ids) && in_array($v['id'], $access_users_ids)) echo 'checked="checked"'; ?>
                                                           value="<?php echo $v['id']; ?>"/> <?php echo $v['realname']; ?>
                                                </label>
                                            <?php } ?>
                                        <?php } ?>
                                    </div>
                                <?php } ?>
                            <?php } ?>
                        </div>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default btns-primary" data-dismiss="modal">取消</button>
                <button type="button" id="set-permission-btn" class="btn btn-primary btns-primary">提交</button>
            </div>
        </div>
        <?php ActiveForm::end() ?>
    </div>
    <?php
}
    ?>

<?php

$css = <<<css

/*访问权限*/

 .jurisdiction input[type="radio"] {
	display: none;
	height: 0;
	width: 0;
}

 .jurisdiction label {
	margin-left: 48px;
	height: 24px;
	position: relative;
	text-indent: 6px;
	cursor: pointer;
	position: relative;
	margin-right: 36px;
}

 .jurisdiction label span {
	display: inline-block;
	height: 24px;
	width: 24px;
	background: url(../../../image/officeHall/uploadAssetPackage/circular1.png) no-repeat center;
	z-index: 2;
	position: absolute;
	left: -22px;
	font-size: 14px;
	color: #adadad;
	font-weight: bold;
}

 .jurisdiction .radioChecked {
	color: #000000;
}

 .jurisdiction .radioChecked span {
	position: absolute;
	left: -32px;
	font-size: 14px;
	margin-right: 17px;
	color: #adadad;
	font-weight: bold;
	margin-right: 57px;
	margin-left: 10px;
	background: url(../../../image/officeHall/uploadAssetPackage/circular2.png) no-repeat left 1px;
	padding-left: 25px;
}


/*//公开部门*/

 .PublicSector>input {
	margin-left: 92px;
}

 .PublicSector #oneSector {
	margin-left: 10px;
}

 .PublicSector .sectorList {
	/*display: none;*/
	width: auto;
	height: auto;
	border: 1px solid #CCCCCC;
	border-radius: 5px;
	margin-left: 50px;
	margin-right: 60px;
	padding: 10px;
}

 .PublicSector .sectorList label {
	margin-right: 10px;
	width: auto;
	font-size: 13px;
}

 .nextStep {
	width: 100%;
	height: 30px;
	margin-top: 40px;
	position: relative;
	margin-bottom: 40px;
}

 .nextStep .btn-primary {
	position: absolute;
	right: 30px;
	bottom: 30px;
}
css;

$this->registerCss($css);

$js = <<<JS


$(":checkbox").change(function() { 
    if($(this).attr("checked")) 
    { 
        $(this).removeAttr("checked"); 
    } 
    else 
    { 
        $(this).attr("checked","true"); 
    } 
});
//此处点击按钮提交数据的jquery
$('#set-permission-btn').click(function () {
  if($("#no_ajax").val()==1){
    $("form").submit();  
  }else{
   $.ajax({
        url:  '/package/set-permissions',
        type: "POST",
        dataType: "json",
        data: $('form').serialize(),
        success: function(Data) {
            if(Data.status){
                layer.msg(Data.msg);
                $('#myModal').modal('hide');
            }else{
                 layer.msg(Data.msg);
            }
        },
        error: function() {
             layer.msg("网络错误！");
        }
    });   
  }

    return false;
});



//访问权限单选按钮样式切换和lable与input关联
$(".jurisdiction").on("click", 'label',
function() {
    if ($(this).prev().val() != 2) {
        $('.modal-body-div').css('height', '100px');
        $('.PublicSector').css('visibility', 'hidden');
    } else {
        $('.PublicSector').css('visibility', 'visible');
        $('.modal-body-div').css('height', 'auto');
    }
    $(this).addClass("radioChecked").siblings().removeClass("radioChecked");

    var lableId = $(this).attr('for');
    //这两行代码可以不用加
    	    $(".jurisdiction input[type='radio']").removeAttr("checked");
    	    $("#"+lableId).attr("checked","checked");
})

//单击公开部门部门选择中的所有input事件
$(".PublicSector").on("click", "input",
function() {
    //heightEqual();
    $('.list_' + $(this).attr("id")).toggle("none");

    var inputId = $(this).attr("id");
    if ($(this).is(':checked')) {
        $('label[for="' + inputId + '"]').css({
            color: "#000000"
        });
    } else {
        $('label[for="' + inputId + '"]').css({
            color: "#adadad"
        });
    }
})

//公开部门单击全选
$('.whole').on("click",
function() {
    var obj = $(this).parent('.sectorList').find('input.access_users');
    if ($(this).is(':checked')) {
        $.each(obj,
        function(i, n) {
            n.checked = true;
            $(i).parent().css({
                color: "#adadad"
            });
        });
    } else {
        $.each(obj,
        function(i, n) {
            n.checked = false;
            $(i).parent().css({
                color: "#000"
            });
        });
    }
})
JS;
$this->registerJs($js);
